// Lindsay Gilmour
// Adam Lusch
// Dan Niceswanger
//
// Computer Graphics Project 1:
// Asteroids!
// A Tribute

#include "Thing.h"

Thing::Thing(b2World* w, int32 nDisplay, int32 m)
	: magic(m)
{
	body = NULL;
	boundingRadius = 0;
	world = w;
	
	numDisplayLists = nDisplay;
	baseDisplayList = glGenLists(numDisplayLists);
}

Thing::~Thing()
{
	world->DestroyBody(body);
	
	if (numDisplayLists > 0)
	{
		glDeleteLists(baseDisplayList, numDisplayLists);
	}
}

void Thing::move(const b2Vec2& newPos)
{
	body->SetXForm(newPos, body->GetAngle());
}

void Thing::rotate(float32 angle)
{
	body->SetXForm(body->GetPosition(), body->GetAngle() + angle);
}

const b2Vec2& Thing::getPosition() const
{
	return body->GetPosition();
}

b2Vec2 Thing::getLinearVelocity() const
{
	return body->GetLinearVelocity();
}

float32 Thing::getBoundingRadius() const
{
	return boundingRadius;
}

float32 Thing::getAngle() const
{
	return body->GetAngle();
}

int32 Thing::getMagic() const
{
	return magic;
}

void Thing::updateBoundingRadius(const b2PolygonDef& poly)
{
	for (int i = 0; i < poly.vertexCount; i++)
	{
		float32 len = poly.vertices[i].Length();
		
		if (len > boundingRadius)
		{
			boundingRadius = len;
		}
	}
}
